package com.doyens.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.entity.*;
import com.doyens.service.TeachingCycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teachingCycle")
@Api(tags = "教学周期管理接口")
public class TeachingCycleController {
    @Autowired
    private TeachingCycleService teachingCycleService;

    @GetMapping("/page")
    @ApiOperation(value = "页面显示教学周期分页信息方法")
    public R<Page> page(int page, int pageSize) {
        //构造分页条件
        Page<TeachingCycle> pageInfo = new Page<>(page, pageSize);
//        Page<ClassDto> classDtoPage=new Page<> (  );
        //条件构造器
        LambdaQueryWrapper<TeachingCycle> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(TeachingCycle::getTeachingCycleId);
        //执行分页
        teachingCycleService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @DeleteMapping
    @ApiOperation(value = "教学周期删除方法")
    @CacheEvict(value = "dept", allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> delete(Integer id) {
        boolean remove = teachingCycleService.removeById(id);
        if (remove){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping
    @ApiOperation(value = "教学周期添加方法")
    public R<String> save(TeachingCycle teachingCycle) {
        boolean save = teachingCycleService.save(teachingCycle);
        if (save){
            return R.success("新增教学周期成功");
        }
        return R.error("添加教学周期失败");
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询教学周期信息方法")
    public R<TeachingCycle> getById(@PathVariable String id) {
        TeachingCycle teachingCycle = teachingCycleService.getById(id);
        if (teachingCycle != null) {
            return R.success(teachingCycle);
        }
        return R.error("没有查询到教学周期信息");
    }

    @PutMapping
    @ApiOperation(value = "修改教学周期信息方法")
    public R<String> update(TeachingCycle teachingCycle) {
        boolean update = teachingCycleService.updateById(teachingCycle);
        if (update){
            return R.success("修改教学周期信息成功");
        }
        return R.error("修改教学周期失败");
    }

    @GetMapping("/list")
    @ApiModelProperty(value = "下拉框展示教学周期列表方法")
    public R<List<TeachingCycle>> list(){
        List<TeachingCycle> list = teachingCycleService.list();
        return R.success(list);
    }
}
